﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility.Ribbon;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using System;

namespace ConfExpert.Forms.Araclar
{
    public partial class EasyNav : XPopupForm
    {
        public EasyNav()
        {
            InitializeComponent();
            Init();
        }

        private void EasyNav_Load(object sender, EventArgs e)
        {
        }

        private void btnCalistir_Click(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            TreeMenu.Nodes.Clear();
            TreeMenu.Columns.Clear();
            TreeListColumn col = new TreeListColumn();
            col.Caption = "Menü";
            //col.VisibleIndex = 1;
            col.Visible = true;

            TreeMenu.Columns.Add(col);

            RibbonControl ribbon = ConfExpertRibbon.rm.RibbonControl;

            foreach (RibbonPage page in ribbon.Pages) //Level 1
            {
                if (page.Visible)
                {
                    TreeListNode nPage = TreeMenu.AppendNode(new object[] { page.Text, null }, null);
                    foreach (RibbonPageGroup group in page.Groups) //Level 2
                    {
                        if (group.Visible)
                        {
                            TreeListNode nGroup = TreeMenu.AppendNode(new object[] { group.Text, null }, nPage);

                            foreach (BarItemLink menu in group.ItemLinks) //Level 3
                            {
                                if (menu.Visible)
                                {
                                    BarButtonItem button = (BarButtonItem)menu.Item;
                                    TreeListNode nItem = TreeMenu.AppendNode(new object[] { button.Caption, button.Glyph }, nGroup);

                                    if (button.ActAsDropDown)
                                    {
                                        BarItemLinkCollection subMenus = ((DevExpress.XtraBars.PopupMenu)button.DropDownControl).ItemLinks;
                                        foreach (BarItemLink subMenu in subMenus) //Level 4
                                        {
                                            BarButtonItem subButton = (BarButtonItem)subMenu.Item;
                                            if (subButton.Visibility == BarItemVisibility.Always)
                                            {
                                                TreeListNode nSubItem = TreeMenu.AppendNode(new object[] { subButton.Caption, button.Glyph }, nItem);
                                                nSubItem.Tag = subButton;
                                            }
                                        }
                                    }
                                    else
                                        nItem.Tag = button;
                                }
                            }
                        }
                    }
                }
            }



            TreeMenu.OptionsBehavior.ReadOnly = true;
            TreeMenu.OptionsBehavior.Editable = false;
            TreeMenu.OptionsView.ShowIndicator = false;
            TreeMenu.OptionsView.ShowColumns = false;

            //TreeMenu.OptionsFind.AlwaysVisible = true;
            //TreeMenu.OptionsFind.AllowFindPanel = true;
            //TreeMenu.OptionsFind.FindMode = FindMode.Always;
            //TreeMenu.OptionsFind.HighlightFindResults = true;
            //TreeMenu.OptionsFilter.FilterMode = FilterMode.Extended;
            //TreeMenu.OptionsBehavior.ExpandNodesOnIncrementalSearch = true;
            //TreeMenu.OptionsBehavior.ExpandNodesOnFiltering = true;

            // panel1.Visible = false;
            TreeMenu.MouseDown += TreeMenu_MouseDown;
            // TreeMenu.Dock = System.Windows.Forms.DockStyle.Bottom;

            TreeMenu.ExpandAll();
        }


        void TreeMenu_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            TreeListHitInfo hitInfo = TreeMenu.CalcHitInfo(e.Location);
            if (hitInfo.HitInfoType == HitInfoType.Cell)
            {
                if (hitInfo.Node.Tag != null)
                {
                    ((BarButtonItem)hitInfo.Node.Tag).PerformClick();
                }
            }
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            string filter = txtFilter.Text;
            filter = "";

            bool found1 = false;
            bool found2 = false;
            bool found3 = false;
            bool found4 = false;

            foreach (TreeListNode level1 in TreeMenu.Nodes)
            {
                foreach (TreeListNode level2 in level1.Nodes)
                {
                    foreach (TreeListNode level3 in level2.Nodes)
                    {
                        foreach (TreeListNode level4 in level3.Nodes)
                        {
                            if (found4)
                            {
                                found3 = true;
                            }

                            if (level4.GetDisplayText(0).Contains(filter))
                            {
                                found4 = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
